package com.yqy.controller;

import com.yqy.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Controller
public class AjaxTestController {
    @PostMapping("/test/ajax")
    public void testAjax(Integer id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("id:"+id);
        ServletInputStream inputStream = request.getInputStream();
        Scanner scanner=new Scanner(inputStream);
        StringBuilder data = new StringBuilder();
        while (scanner.hasNext()){
            data.append(scanner.next());
        }
        System.out.println(data.toString());
        response.getWriter().write("hello,axios");
    }

    @PostMapping("/test/RequestBody/form")
    public String testRequestBodyForm(@RequestBody String requestBody){
        System.out.println("requestBody:"+requestBody);
        return "success";
    }

    //将json格式的数据转换为实体类对象
    //requestBody:User{id=null, username='admin', password='123456', age=19, gender='男'}
    @PostMapping("/test/RequestBody/json")
    /*public void testRequestBodyJson(@RequestBody User user, HttpServletResponse response) throws IOException {
        System.out.println("requestBody:" + user);
        response.getWriter().write("hello,RequestBodyJson");
    }*/

    //将json格式的数据转换为Map对象
    //requestBody:{username=admin, password=123456, age=19, gender=男}
    public void testRequestBodyJson(@RequestBody Map<String,Object> map, HttpServletResponse response) throws IOException {
        System.out.println("requestBody:" + map);
        response.getWriter().write("hello,RequestBodyJson");
    }

    /*@RequestMapping("/test/ResponseBody")
    public String testResponseBody(){
        //此时会跳转到逻辑视图success所对应的页面
        return "success";
    }*/

    @RequestMapping("/test/ResponseBody")
    @ResponseBody
    public String testResponseBody(){
        //此时响应浏览器数据success
        return "success";
    }

    @RequestMapping("/test/ResponseBody/json")
    @ResponseBody
    //测试list集合转换为json数组
    public List<User> testResponseBodyJson(){
        User user1 = new User(1001, "admin1", "123456", 20, "男");
        User user2 = new User(1002, "admin2", "123456", 20, "男");
        User user3 = new User(1003, "admin3", "123456", 20, "男");
        List<User> list = Arrays.asList(user1, user2, user3);
        return list;
    }

    /*//测试map集合转换为json对象
    public Map<Integer,Object> testResponseBodyJson(){
        User user1 = new User(1001, "admin", "123456", 20, "男");
        User user2 = new User(1002, "admin", "123456", 20, "男");
        User user3 = new User(1003, "admin", "123456", 20, "男");
        Map<Integer,Object> map=new HashMap<>();
        map.put(1001,user1);
        map.put(1002,user2);
        map.put(1003,user3);
        return map;
    }*/

    /*//测试实体类转换为json对象
    public User testResponseBodyJson(){
        User user = new User(1001, "admin", "123456", 20, "男");
        return user;
        //{"id":1001,"username":"admin","password":"123456","age":20,"gender":"男"}
    }*/
}
